<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('关联字段配置')" />
	<style type="text/css">
	.layui-col-md4 {width: 29%;min-width: 200px !important;}
	</style>
</head>
<body>
<div style="padding: 0 5px;">
	<div class="layui-inline layui-col-md5">
		<div class="layui-inline layui-col-md11">
			<blockquote class="layui-elem-quote" style="padding-top: 10px;"><div style="float:left;margin: 0px 10px 0px -10px;"><br/><br/><br/>源<br/>数<br/>据<br/>表<br/>字<br/>段</div>
				<table class="layui-inline layui-hide" id="dhtable2" lay-filter="ftable2"></table>
			</blockquote>
		</div>
		<div class="layui-inline layui-col-md1">
			<div class="layui-btn movebtn layui-btn-disabled" style="margin-top: 255px;padding:0 10px">
	            <p class="dhicon dh-angle-double-right" style="font-size:14px;margin:0;color:#C9C9C9"></p>
	        </div>
		</div>
		<div class="layui-inline layui-col-md11">
			<blockquote class="layui-elem-quote" style="padding-top: 10px;"><div style="float:left;margin: 0px 10px 0px -10px;"><br/><br/><br/>目<br/>标<br/>数<br/>据<br/>表<br/>字<br/>段</div>
				<table class="layui-hide" id="dhtable3" lay-filter="ftable3"></table>
			</blockquote>
		</div>
	</div>
	<div class="layui-inline layui-col-md7">
		<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
	</div>
</div>
<script type="text/html" id="move">	
	<a class="layui-btn layui-btn-xs dhicon dh-angle-up" style="padding: 0 3px;" lay-event="up"></a>
	<a class="layui-btn layui-btn-xs dhicon dh-angle-down" style="padding: 0 3px;margin-left:0px;" lay-event="down"></a>   
</script>
<script type="text/html" id="barDh">
    <a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" lay-event="del">&nbsp;删除</a>	
</script>
<script type="text/html" id="templet_bFieldKeys">
    <input type="checkbox" value="{{d.LAY_TABLE_INDEX}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="bFieldKeys" {{ d.bFieldKeys? 'checked' : '' }}>
</script>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "system/dictionaryrelationforfield";
	var prefix2 = ctx + "system/dictionaryforfield";
	/*]]>*/
</script>
<script type="text/javascript">
var $,layer,form,table,listData,delData=new Array();
layui.use(['table','form','layer','jquery'], function () {
     $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table;
     $.ajax({
  		url: prefix + '/list?pguid='+window.location.href.split("=")[1],
        type: "get",
        dataType: "json",
        success: function(result) {
          	listData=result.data;
          	//表单初始化
          	$.tablelist.init({
          		elem: '#dhtable'
				,data:listData
				,limit:1000
				,page:false
  		 		,cols: [[
	  	    		{type:'numbers', fixed: 'left'}
	  	    		,{field:'sFieldName', title: '源列名',align: 'center',edit:'text'}
	  	    		,{field:'sFields', title: '源字段',align: 'center',edit:'text'}
	  	    		,{field:'rFieldName', title: '目标列名',align: 'center',edit:'text'}
	  	    		,{field:'rFields', title: '目标字段',align: 'center',edit:'text'}
	  	    		,{title: '移动',align: 'center', toolbar: '#move',width:70,hide:true}
	  	    		,{field:'bFieldKeys',title: '主键列',align: 'center',templet:'#templet_bFieldKeys'}
	  	    		,{fixed: 'right', title: '操作',align: 'center', toolbar: '#barDh'}
				]
				]
               ,height:'full-36'
               ,width:695
           });
        }
   	});
     $.tablelist.init({
        elem: '#dhtable2'
        ,url: prefix2 + "/tablelist?moduleTable="+window.location.href.split("=")[2]
	 	,limit:1000
	 	,page:false
	 	,initSort: {field: 'fields',type: 'asc'}
		,where: {
	    	field: 'fields' //排序字段
	    	,order: 'asc' //排序方式
	    }
	 	,cols: [[
 				{type:'numbers', fixed: 'left'}
    			,{type:'checkbox'}
	    		,{field:'fieldName', title: '源列名',align: 'center',templet:function(d){
	    			if(d.fieldAlias)return d.fieldAlias;else return d.fieldName;
	    		}}
	    		,{field:'fields', title: '源字段',align: 'center'}
		 ]
		 ]
        ,height:'full-330'
        ,width:425
     });
     $.tablelist.init({
        elem: '#dhtable3'
        ,url: prefix2 + "/tablelist?moduleTable="+window.location.href.split("=")[3]
		,limit:1000
		,page:false	 
		,initSort: {field: 'fields',type: 'asc'}
		,where: {
	    	field: 'fields' //排序字段
	    	,order: 'asc' //排序方式
	    }
     	,cols: [[
			 	{type:'numbers', fixed: 'left'}
	    		,{type:'checkbox'}
	    		,{field:'fieldName', title: '目标列名',align: 'center',templet:function(d){
	    			if(d.fieldAlias)return d.fieldAlias;else return d.fieldName;
	    		}}
	    		,{field:'fields', title: '目标字段',align: 'center'}
		 ]
		 ]
        ,height:'full-330'
        ,width:425
    });
   	//监听开关动作
	form.on('switch(bFieldKeys)', function(obj){
		table.cache.dhtable[obj.value].bFieldKeys=obj.elem.checked;
    });
   	//监听行事件
 	table.on('tool(ftable)', function (obj) {
 		var data = obj.data, event = obj.event,rFieldName,rFields,trIndex=parseInt(obj.tr[0].dataset.index);//当前行下标
		listData=table.cache.dhtable;
 		switch(event){
 			case "del":
 				obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
 				data['del']=1;
 				if(!data['newData'])delData.push(data);
 				for(var i=0; i < listData.length; i++){
 					if(!listData[i] || listData[i].length==0){//不存在和array[0]
 						listData.splice(i, 1);    //删除一项
 					}
 					continue;
 				}
				table.reload('dhtable',{data : listData});
 				break;
 			case "up":
 				if(0==trIndex){
 					layer.msg("已经到顶啦！");
 				}else{
 					rFieldName=listData[trIndex-1].rFieldName;
 					rFields=listData[trIndex-1].rFields;
 					listData[trIndex-1].rFieldName=listData[trIndex].rFieldName;
 					listData[trIndex-1].rFields=listData[trIndex].rFields;
 					listData[trIndex].rFieldName=rFieldName;
 					listData[trIndex].rFields=rFields;
 					table.reload('dhtable',{data : listData});
 				}
 				break;
 			case "down":
 				if(listData.length-1==trIndex){
 					layer.msg("已经到底啦！");
 				}else{
 					rFieldName=listData[trIndex+1].rFieldName;
 					rFields=listData[trIndex+1].rFields;
 					listData[trIndex+1].rFieldName=listData[trIndex].rFieldName;
 					listData[trIndex+1].rFields=listData[trIndex].rFields;
 					listData[trIndex].rFieldName=rFieldName;
 					listData[trIndex].rFields=rFields;
 					table.reload('dhtable',{data : listData});
 				}
 				break;
 		}
 	});
   	//点击复选框阻止冒泡
    $(document).on("click",".layui-form-checkbox",function(e){
    	e.stopPropagation();
        var checkStatus2 = table.checkStatus('dhtable2');
        var checkStatus3 = table.checkStatus('dhtable3');
        var data = checkStatus2.data, data2 = checkStatus3.data;
        if(data.length > 0 || data2.length > 0){
        	$(".movebtn").removeClass("layui-btn-disabled");
        	$(".movebtn").children().css('color','#fff');
    	}else{
    		$(".movebtn").addClass("layui-btn-disabled");
    		$(".movebtn").children().css('color','#C9C9C9');
   		}
    });
   	//数据关联移入
    $('.movebtn').click(function(){
    	var data =table.cache.dhtable,data2 = table.checkStatus('dhtable2').data,data3 = table.checkStatus('dhtable3').data;
   		$.each(data2,function(i,v){
   			if(v.fieldAlias)v.fieldName=v.fieldAlias;
   			data.push({"sFields":v.fields,"sFieldName":v.fieldName,"newData":1,"pguid":window.location.href.split("=")[1],"bFieldKeys":0});
       	});
   		$.each(data,function(i,v){
   			if(!v.rFields && data3.length!=0){
   				v.rFields=data3[0].fields;
   				if(data3[0].fieldAlias)data3[0].fieldName=data3[0].fieldAlias;
				v.rFieldName=data3[0].fieldName;
				data3.splice(0,1);
   			}
       	});
   		table.reload('dhtable',{data:data});
       	$('div.laytable-cell-checkbox').find('div.layui-form-checked').click();
       	$(".movebtn").addClass("layui-btn-disabled");
       	$(".movebtn").children().css('color','#C9C9C9');
    })
});
function callbackdata(){
	var flag=false;
	$.each(table.cache.dhtable,function(i,v){
		$.each(table.cache.dhtable,function(i2,v2){
			if(v.rFields==v2.rFields && v.sFields==v2.sFields && i!=i2)flag=true;
		});
	});
	if(flag){
		return "重复";
	}else{
		$.each(delData,function(i,v){
			table.cache.dhtable.push(v);
		});
		return table.cache.dhtable;
	}
}
</script>
</body>
</html>